clear all
set more off
program drop _all
macro drop _all

cd "C:\Users\75618rpo\Dropbox\Research\Literature Latex\Corruption, accountability and voting\Submission R&R JoP\Replication\replication_package_JOP"

use "data_brasil_camp.dta", clear

	global muni_controls lpib_pc gini illiteracy share_urb radio_am 
		label var lpib_pc "GDP per capita (in ln)"
		label var radio_am "AM Radio Station"
		label var gini "Gini index"
		label var illiteracy "Illiteracy rate"
		label var share_urb	"Share urban population"
	
	global mayor_controls male married age education prev_mayor share_vote_margin dum_PT
		label var prev_mayor "Previous mayor"
		label var dum_PT "Workers' Party dummy"
		
	global challengers_outcomes total_cand mean_educ mean_skilled mean_age 
	global challenger_controls male married age education prev_mayor dum_PT
	global cluster id_city

	label var month0_3 "Before 3 months"
	label var month3_12 "Between 3 to 12 months"
	label var month12_n "Over 12 months"	
	
cls
* Reputation shocks measures (corruption)
		*(1) Corruption
		label var before "Before"
		capture gen before_broad = before * broad
				label var before_broad "Before $\times$ corruption"
				label var broad "Corruption"
				
		*(2) Fraction
		capture gen corrupt_before = before * fraction_broad
				label var corrupt_before "Before $\times$ share corruption"
				label var fraction_broad "Share corruption"
		
		*(3) Median
		capture gen above_median_corrupt = .
		quietly sum fraction_broad if term == 2001, d
		capture replace above_median_corrupt = (fraction_broad> r(p50)) if fraction_broad ~= . & term == 2001
		quietly sum fraction_broad if term == 2005, d
		capture replace above_median_corrupt = (fraction_broad> r(p50)) if fraction_broad ~= . & term == 2005
			label var above_median_corrupt "Above median corruption" 

		capture gen median_before = before * above_median_corrupt
				label var median_before "Before $\times$ Above median corruption"
		
		*(4) Quartiles
		  capture drop quart*
		  
		  xtile quartile_broad2001 = fraction_broad if term == 2001, nquantiles(4)
		  xtile quartile_broad2005 = fraction_broad if term == 2005, nquantiles(4)
		  gen quartile_broad = quartile_broad2001 if term == 2001
		  replace quartile_broad = quartile_broad2005 if term == 2005
			label var quartile_broad "Corresponding quartile associated with fraction_broad"
			
		  tabulate quartile_broad, gen(quartile)
	
		  gen quart2_before = before * quartile2
		  gen quart3_before = before * quartile3
		  gen quart4_before = before * quartile4
				label var quart2_before "Before $\times$ 2nd quartile of corruption"
				label var quart3_before "Before $\times$ 3rd quartile of corruption"
				label var quart4_before "Before $\times$ 4th quartile of corruption"
				label var quartile1 "1st quartile of corruption"
				label var quartile2 "2nd quartile of corruption"
				label var quartile3 "3rd quartile of corruption"
				label var quartile4 "4th quartile of corruption"	
				
				label var broad "Corruption"
				label var fraction_broad "Share corruption"

		capture drop month0_3 month3_12 month12_n 
		
		gen month0_3 = (lottery == 8 | lottery == 9 )
		gen month3_12 = (lottery >= 4 & lottery <= 7 )
			replace month3_12 = (lottery >=23 & lottery <=25 ) if term == 2005
		gen month12_n = (lottery <=3)
			replace month12_n = 1 if term == 2005 & before == 1 & lottery< 23

			label var month0_3 "Less than 3 months before the election"
			label var month3_12 "Betweem 3 to 12 months before the election"
			label var month12_n "More than 12 months before the election"

		capture drop median_month0_3 median_month3_12 median_year12_n
		
		gen median_month0_3 = above_median_corrupt *  month0_3 
		gen median_month3_12 = above_median_corrupt *  month3_12 
		gen median_year12_n =  above_median_corrupt *  month12_n

			label var median_month0_3 "Before 3 months $\times$ Above median corruption"
			label var median_month3_12 "Between 3-12 months $\times$ Above median corruption"
			label var median_year12_n "Over 12 months $\times$ Above median corruption"

				
	global corruption_vars broad fraction_broad above_median_corrupt quartile1 quartile2 quartile3 quartile4
	


* Table 1. Summary statistics Corruption measure.
		tabstat broad if term == 2001, s(N mean) c(s) format (%6.2f)
		tabstat broad if term == 2005, s(N mean) c(s) format (%6.2f)
		
		tabstat fraction_broad if term == 2001, s(N mean p25 median p75 max) c(s) format (%6.2f)
		tabstat fraction_broad if term == 2005, s(N mean p25 median p75 max) c(s) format (%6.2f)

		
* Figure 1
preserve 
	collapse date_lot before month* year* (count) number=sample_camp_exp if sample_camp_exp == 1, by(term lottery)

	gen election_date = "2001-2004 term" if term == 2001
		replace election_date = "2005-2008 term" if term == 2005
		
	twoway (bar number date_lot, barwidth(15)) (bar number date_lot if before == 1, barwidth(15)), by(election_date, note("") legend(pos(6))) xline(16347 17815 , lwidth(medthick) lpattern(dash) lcolor(gs8)) graphregion(fcolor(white) lcolor(white))  xtitle("") ytitle("Municipalities")   legend( label(1 "Information after") label(2 "Information before") bmargin(vsmall) col(2)) xlabel(, format(%td) labsize(vsmall))   
			graph export "tables and figures/figure_1.pdf", replace
restore		
		

* Table 2. Balance Test for Incumbents
preserve
	global size_column "{p{5cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1.5cm}p{1.5cm}c}"
	global format_table "vce(cluster $cluster) wide(mean1 sd1 mean2 sd2 diff pval) pvalues varlabels  format(%9.2f) nostars nonumbers noobservations nolines"

	replace radio_am = 100 if radio_am == 1	/*so it is in %*/
	
	balancetable before broad fraction_broad using "tables and figures/table_2a.tex" if sample_camp_exp == 1, $format_table replace prehead("\begin{tabularx}{\linewidth} $size_column \textit{A. Reputation shocks} \\") postfoot("\end{tabularx}") 
	
	balancetable before $mayor_controls using "tables and figures/table_2b.tex" if sample_camp_exp == 1, $format_table replace prehead("\begin{tabularx}{\linewidth}  $size_column {\textit{B. Mayor characteristics}} \\") postfoot("\end{tabularx}") 

	balancetable before $muni_controls using "tables and figures/table_2c.tex" if sample_camp_exp == 1, $format_table replace prehead("\begin{tabularx}{\linewidth} $size_column {\textit{C. Municipal characteristics}} \\") postfoot("\end{tabularx}") 	
restore

* Table 3. Balance Test for Challenger. 
preserve
use "data_brasil_camp_chall.dta", clear

		label var lpib_pc "GDP per capita (in ln)"
		label var radio_am "AM Radio Station"
		label var gini "Gini index"
		label var illiteracy "Illiteracy rate"
		label var share_urb	"Share urban population"
		label var prev_mayor "Previous mayor"
		label var male "Male"
		label var married "Married"
		label var age "Age"
		label var education "Education"
		label var dum_PT "Workers' Party dummy"
		label var broad "Corruption"
		label var fraction_broad "Share corruption"

	keep if sample_camp_exp_chal == 1
	tab before
	tab before if fraction_broad !=.
	replace radio_am = 100 if radio_am == 1	/*so it is in %*/
	
	global size_column "{p{5cm}p{1cm}p{1cm}p{1cm}p{1cm}p{1.5cm}p{1.5cm}c}"
	global format_table "vce(cluster $cluster) wide(mean1 sd1 mean2 sd2 diff pval) pvalues varlabels  format(%9.2f) nostars nonumbers noobservations nolines"
	
	balancetable before broad fraction_broad using "tables and figures/table_3a.tex", $format_table replace prehead("\begin{tabularx}{\linewidth} $size_column \textit{A. Reputation shocks} \\") postfoot("\end{tabularx}") 
	
	balancetable before $challenger_controls using "tables and figures/table_3b.tex", $format_table replace prehead("\begin{tabularx}{\linewidth}  $size_column {\textit{B. Challenger characteristics}} \\") postfoot("\end{tabularx}") 

	balancetable before $muni_controls using "tables and figures/table_3c.tex", $format_table replace prehead("\begin{tabularx}{\linewidth} $size_column {\textit{C. Municipal characteristics}} \\") postfoot("\end{tabularx}") 	
	
restore

	
	
* Table 4.
eststo clear	
preserve
keep if sample_camp_exp == 1

	* 0. Baseline: Average causal Impact of Information Disclosure.
		eststo reg_broad_basic: quietly areg camp_exp_pc before i.term , absorb(codigo_uf) robust cluster($cluster)
				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_broad_basic

		* 0.1. baseline + controls
		eststo reg_broad_basic_c: quietly areg camp_exp_pc before $muni_controls $mayor_controls i.term , absorb(codigo_uf) robust cluster($cluster)
				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_broad_basic_c
				estadd local mayor_municipal "\checkmark": reg_broad_basic_c

	
	* 1. Broad				
		eststo reg_broad: quietly areg camp_exp_pc before broad before_broad i.term , absorb(codigo_uf) robust cluster($cluster)
				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_broad
				lincom before + before_broad
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': reg_broad
				estadd local p_value `p_test': reg_broad

		*1.2. broad + controls			
		eststo reg_broad_c: quietly areg camp_exp_pc before broad before_broad  $muni_controls $mayor_controls i.term , absorb(codigo_uf) robust cluster($cluster)
				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_broad_c
				estadd local mayor_municipal "\checkmark": reg_broad_c
				lincom before + before_broad
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': reg_broad_c
				estadd local p_value `p_test': reg_broad_c				

	* 2. fraction_broad		
		eststo reg_fraction: quietly areg camp_exp_pc before fraction_broad corrupt_before i.term, absorb(codigo_uf) robust  cluster($cluster)
				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_fraction
				sum fraction_broad if e(sample), mean 
				loc my_mean: display %9.3f r(mean)				
				lincom before + corrupt_before * `my_mean'	
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': reg_fraction
				estadd local p_value `p_test': reg_fraction		
				
		*2.2 fraction_broad + controls			
		eststo reg_fraction_c: quietly areg camp_exp_pc before fraction_broad corrupt_before $muni_controls $mayor_controls  i.term, absorb(codigo_uf) robust  cluster($cluster)
				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_fraction_c
				estadd local mayor_municipal "\checkmark": reg_fraction_c
				sum fraction_broad if e(sample), mean 
				loc my_mean: display %9.3f r(mean)				
				lincom before + corrupt_before * `my_mean'	
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': reg_fraction_c
				estadd local p_value `p_test': reg_fraction_c
								
	* 3. above_median_broad 							
		eststo reg_median: quietly areg camp_exp_pc before above_median_corrupt median_before i.term, absorb(codigo_uf) robust  cluster($cluster)
				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_median
				lincom before + median_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': reg_median
				estadd local p_value `p_test': reg_median

		*3.2. above_median_broad + controls	
		eststo reg_median_c: quietly areg camp_exp_pc before above_median_corrupt median_before $muni_controls $mayor_controls i.term, absorb(codigo_uf) robust  cluster($cluster)
				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_median_c
				estadd local mayor_municipal "\checkmark": reg_median_c
				lincom before + median_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': reg_median_c
				estadd local p_value `p_test': reg_median_c

	* 4. quartiles broad
		eststo reg_quart:   quietly areg camp_exp_pc before quartile2 quartile3 quartile4 quart2_before quart3_before quart4_before i.term, absorb(codigo_uf) robust cluster($cluster)
				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_quart
				* 2nd quartile
				lincom before + quart2_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2q2 `estimate': reg_quart
				estadd local p_valueq2 `p_test': reg_quart		
				* 3rd quartile
				lincom before + quart3_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2q3 `estimate': reg_quart
				estadd local p_valueq3 `p_test': reg_quart
				* 4th quartile
				lincom before + quart4_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2q4 `estimate': reg_quart
				estadd local p_valueq4 `p_test': reg_quart 
				
		*4.2 quartiles broad + controls
		eststo reg_quart_c: quietly areg camp_exp_pc before quartile2 quartile3 quartile4 quart2_before quart3_before quart4_before $muni_controls $mayor_controls   i.term, absorb(codigo_uf) robust  cluster($cluster)
				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_quart_c
				estadd local mayor_municipal "\checkmark": reg_quart_c
				* 2nd quartile
				lincom before + quart2_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2q2 `estimate': reg_quart_c
				estadd local p_valueq2 `p_test': reg_quart_c		
				* 3rd quartile
				lincom before + quart3_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2q3 `estimate': reg_quart_c
				estadd local p_valueq3 `p_test': reg_quart_c
				* 4th quartile
				lincom before + quart4_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2q4 `estimate': reg_quart_c
				estadd local p_valueq4 `p_test': reg_quart_c 
					
* EXPORT TABLE TO LATEX		
		#delimit;
			esttab reg_broad_basic reg_broad_basic_c reg_broad reg_broad_c reg_fraction reg_fraction_c reg_median reg_median_c reg_quart reg_quart_c
			using "tables and figures/table_4.tex", b(3) se compress label star(* 0.10 ** 0.05 *** 0.01) replace
			mgroups( "\shortstack{Information \\ disclosure}" "Corruption"   "Share corruption"  "\shortstack{Median \\ Corruption}"   "\shortstack{Quartiles\\ Corruption}"  , pattern(1 0 1 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
			keep(before_broad before broad corrupt_before fraction_broad median_before above_median_corrupt quartile2 quartile3 quartile4 quart2_before quart3_before quart4_before ) 
			stats(N mD mayor_municipal state_fe r2_a b0_b2 p_value b0_b2q2 p_valueq2 b0_b2q3 p_valueq3 b0_b2q4 p_valueq4, fmt(%9.0fc %9.3fc ) labels("Observations" "Dep. Var. Mean" "Mayor and municipal controls" "State and term FE" "Adjusted R$^2$" "$\beta_0+\beta_2 \, c_{ist}$" "p-value" "$\beta_0+\beta_2 \, c_{ist}$ (quartile 2)" "p-value" "$\beta_0+\beta_2 \, c_{ist}$ (quartile 3)" "p-value" "$\beta_0+\beta_2 \, c_{ist}$ (quartile 4)" "p-value"))
			/*nonum*/ ar2 booktabs nonotes nomtitles title(deletethis) 
			substitute(\begin{table}[htbp]\centering \centering \caption{deletethis} /*\footnotesize*/{ \end{tabular} \end{tabular}} \end{table} %);
		#delimit cr
restore
		
		

* Table 5.
preserve
cls		
eststo clear	
use "data_brasil_camp_chall.dta", clear

	global dep_var camp_exp_pc
	global BEFORE before
	global corrupt broad
	
	sum fraction_$corrupt
	capture gen av_fraction_$corrupt = r(mean)

	
	* 0. Baseline: 
		eststo reg_broad_basic: quietly areg $dep_var $BEFORE i.term , absorb(codigo_uf) robust cluster(id_city)
				sum $dep_var if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_broad_basic

		*0.1. baseline + controls
		eststo reg_broad_basic_c: quietly areg $dep_var $BEFORE $muni_controls $challenger_controls i.term , absorb(codigo_uf) robust cluster(id_city)
				sum $dep_var if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_broad_basic_c
				estadd local mayor_municipal "\checkmark": reg_broad_basic_c

	* 1. Broad
		capture gen before_$corrupt = $BEFORE * $corrupt
				label var before_$corrupt "Before $\times$ corruption"
				label var $corrupt "Corruption"
				
		eststo reg_broad: quietly areg $dep_var $BEFORE $corrupt before_$corrupt i.term , absorb(codigo_uf) robust cluster(id_city)
				sum $dep_var if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_broad
				test $BEFORE + before_$corrupt == 0	
				lincom $BEFORE + before_$corrupt
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': reg_broad
				estadd local p_value `p_test': reg_broad

		*1.2. broad + controls			
		eststo reg_broad_c: quietly areg $dep_var $BEFORE $corrupt before_$corrupt  $muni_controls $challenger_controls i.term , absorb(codigo_uf) robust cluster(id_city)
				sum $dep_var if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_broad_c
				estadd local mayor_municipal "\checkmark": reg_broad_c
				lincom $BEFORE + before_$corrupt
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': reg_broad_c
				estadd local p_value `p_test': reg_broad_c

	* 2. fraction_broad
		capture gen corrupt_before = $BEFORE * fraction_$corrupt
				label var corrupt_before "Before $\times$ share corruption"
				label var fraction_$corrupt "Share corruption"
		
		eststo reg_fraction: quietly areg $dep_var $BEFORE fraction_$corrupt corrupt_before i.term, absorb(codigo_uf) robust  cluster(id_city)
				sum $dep_var if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_fraction
				sum fraction_$corrupt if e(sample), mean 
				loc my_mean: display %9.3f r(mean)				
				lincom $BEFORE + corrupt_before * `my_mean'	
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': reg_fraction
				estadd local p_value `p_test': reg_fraction
				
		*2.2 fraction_broad + controls			
		eststo reg_fraction_c: quietly areg $dep_var $BEFORE fraction_$corrupt corrupt_before $muni_controls $challenger_controls  i.term, absorb(codigo_uf) robust  cluster(id_city)
				sum $dep_var if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_fraction_c
				estadd local mayor_municipal "\checkmark": reg_fraction_c
				sum fraction_$corrupt if e(sample), mean 
				loc my_mean: display %9.3f r(mean)				
				lincom $BEFORE + corrupt_before * `my_mean'	
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': reg_fraction_c
				estadd local p_value `p_test': reg_fraction_c
								
	* 3. above_median_broad 				
		local dep_var camp_exp_pc
		eststo reg_median: quietly areg $dep_var $BEFORE above_median_corrupt median_before i.term, absorb(codigo_uf) robust  cluster(id_city)
				sum $dep_var if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_median
				lincom $BEFORE + median_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': reg_median
				estadd local p_value `p_test': reg_median

		*3.2. above_median_broad + controls
		eststo reg_median_c: quietly areg $dep_var $BEFORE above_median_corrupt median_before $muni_controls $challenger_controls i.term, absorb(codigo_uf) robust  cluster(id_city)
				sum $dep_var if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_median_c
				estadd local mayor_municipal "\checkmark": reg_median_c
				lincom $BEFORE + median_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': reg_median_c
				estadd local p_value `p_test': reg_median_c
				
				
	* 4. quartiles broad
			
		eststo reg_quart:   quietly areg $dep_var $BEFORE quartile2 quartile3 quartile4 quart2_before quart3_before quart4_before i.term, absorb(codigo_uf) robust cluster(id_city)
				sum $dep_var if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_quart
				estadd local p_value `p_test': reg_quart
				* 2nd quartile
				lincom before + quart2_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2q2 `estimate': reg_quart
				estadd local p_valueq2 `p_test': reg_quart		
				* 3rd quartile
				lincom before + quart3_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2q3 `estimate': reg_quart
				estadd local p_valueq3 `p_test': reg_quart
				* 4th quartile
				lincom before + quart4_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2q4 `estimate': reg_quart
				estadd local p_valueq4 `p_test': reg_quart 

		*4.2 quartiles broad + controls
		eststo reg_quart_c: quietly areg $dep_var $BEFORE quartile2 quartile3 quartile4 quart2_before quart3_before quart4_before $muni_controls $challenger_controls   i.term, absorb(codigo_uf) robust  cluster(id_city)
				sum $dep_var if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": reg_quart_c
				estadd local mayor_municipal "\checkmark": reg_quart_c
				* 2nd quartile
				lincom before + quart2_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2q2 `estimate': reg_quart_c
				estadd local p_valueq2 `p_test': reg_quart_c		
				* 3rd quartile
				lincom before + quart3_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2q3 `estimate': reg_quart_c
				estadd local p_valueq3 `p_test': reg_quart_c
				* 4th quartile
				lincom before + quart4_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2q4 `estimate': reg_quart_c
				estadd local p_valueq4 `p_test': reg_quart_c 			

label var before "Before"	
		#delimit;
			esttab reg_broad_basic reg_broad_basic_c reg_broad reg_broad_c reg_fraction reg_fraction_c reg_median reg_median_c reg_quart reg_quart_c
			using "tables and figures/table_5.tex", b(3) se compress label star(* 0.10 ** 0.05 *** 0.01) replace
			mgroups( "\shortstack{Information \\ disclosure}" "Corruption"   "Share corruption"  "\shortstack{Median \\ Corruption}"   "\shortstack{Quartiles\\ Corruption}"  , pattern(1 0 1 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
			keep(before_$corrupt $BEFORE $corrupt corrupt_before fraction_$corrupt median_before above_median_corrupt quartile2 quartile3 quartile4 quart2_before quart3_before quart4_before ) 
			stats(N mD mayor_municipal state_fe r2_a b0_b2 p_value b0_b2q2 p_valueq2 b0_b2q3 p_valueq3 b0_b2q4 p_valueq4, fmt(%9.0fc %9.3fc ) labels("Observations" "Dep. Var. Mean" "Mayor and municipal controls" "State and term FE" "Adjusted R$^2$" "$\beta_0+\beta_2 \, c_{ist}$" "p-value" "$\beta_0+\beta_2 \, c_{ist}$ (quartile 2)" "p-value" "$\beta_0+\beta_2 \, c_{ist}$ (quartile 3)" "p-value" "$\beta_0+\beta_2 \, c_{ist}$ (quartile 4)" "p-value"))
			/*nonum*/ ar2 booktabs nonotes nomtitles title(deletethis) 
			substitute(\begin{table}[htbp]\centering \centering \caption{deletethis} /*\footnotesize*/{ \end{tabular} \end{tabular}} \end{table} %);
		#delimit cr
restore

			

* Table 6.
preserve
keep if sample_camp_exp == 1
 
	global BEFORE before
	global corrupt broad
	
	sum share_vote_margin if sample_camp_exp == 1,d
	capture gen above_share_vote_margin = (share_vote_margin > r(p50))  if share_vote_margin ~= . & sample_camp_exp == 1
	
	capture gen above_margin = above_share_vote_margin
	global var_diff above_margin 
	local reg $var_diff
	
foreach num of numlist 0 1  {
	
	*3. above_median_broad 
			eststo `reg'_m_`num': quietly areg camp_exp_pc $BEFORE above_median_corrupt median_before i.term if $var_diff == `num', absorb(codigo_uf) robust  cluster(id_city)
				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": `reg'_m_`num'
				lincom $BEFORE + median_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': `reg'_m_`num'
				estadd local p_value `p_test': `reg'_m_`num'
				
		*3.2. above_median_broad + controls
			eststo `reg'_m_`num'_c: quietly areg camp_exp_pc $BEFORE above_median_corrupt median_before $muni_controls $mayor_controls i.term if $var_diff == `num', absorb(codigo_uf) robust  cluster(id_city)
				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": `reg'_m_`num'_c
				estadd local mayor_municipal "\checkmark": `reg'_m_`num'_c
				lincom $BEFORE + median_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': `reg'_m_`num'_c
				estadd local p_value `p_test': `reg'_m_`num'_c
			 
					
	*4. quartiles broad	
			eststo `reg'_q_`num':   quietly areg camp_exp_pc $BEFORE quartile2 quartile3 quartile4 quart2_before quart3_before quart4_before i.term i.term if $var_diff == `num', absorb(codigo_uf) robust cluster(id_city)
 				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": `reg'_q_`num'
				lincom $BEFORE + quart4_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': `reg'_q_`num'
				estadd local p_value `p_test': `reg'_q_`num'

		*4.2 quartiles broad + controls
			eststo `reg'_q_`num'_c: quietly areg camp_exp_pc $BEFORE quartile2 quartile3 quartile4 quart2_before quart3_before quart4_before $muni_controls $mayor_controls i.term if $var_diff == `num', absorb(codigo_uf) robust  cluster(id_city)
			 	sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": `reg'_q_`num'_c
				estadd local mayor_municipal "\checkmark": `reg'_q_`num'_c
				lincom $BEFORE + quart4_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': `reg'_q_`num'_c
				estadd local p_value `p_test': `reg'_q_`num'_c

}	

local reg above_margin 
    		#delimit;
			esttab `reg'_m_0 `reg'_m_0_c
				   `reg'_q_0 `reg'_q_0_c
				   `reg'_m_1 `reg'_m_1_c
				   `reg'_q_1 `reg'_q_1_c
			using "tables and figures/table_6.tex", b(3) se compress label star(* 0.10 ** 0.05 *** 0.01) replace
			mtitles( "Median" "Median" "Quartile"  "Quartile"  "Median"  "Median"  "Quartile"  "Quartile" ) 
			keep($BEFORE above_median_corrupt median_before quart2_before quart3_before quart4_before quartile2 quartile3 quartile4)
			mgroups( "\shortstack{Weak incumbent\\(head start $\approx$ 0)}"  "\shortstack{Strong Incumbent \\  (head start $>>$ 0)}", pattern(1 0 0 0 1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
 			stats(N mD mayor_municipal state_fe r2_a b0_b2 p_value, fmt(%9.0fc %9.3fc ) labels("Observations" "Dep. Var. Mean" "Mayor and municipal controls" "State and term FE" "Adjusted R$^2$" "$\beta_0+\beta_2 \, c_{ist}$" "p-value"))
			/*nonum*/ ar2 booktabs nonotes /*nomtitles*/ title(deletethis) 
			substitute(\begin{table}[htbp]\centering \centering \caption{deletethis} /*\footnotesize*/{ \end{tabular} \end{tabular}} \end{table} %);
			#delimit cr
restore
 

   
			
* Table 7.
preserve
use "data_brasil_camp_chall.dta", clear		

 	global BEFORE before
	global corrupt broad
	
	global var_diff above_margin 
	local reg $var_diff
foreach num of numlist 0 1  {
	
			eststo `reg'_m_`num':  quietly areg camp_exp_pc $BEFORE above_median_corrupt median_before i.term if $var_diff == `num', absorb(codigo_uf) robust  cluster(id_city)
				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": `reg'_m_`num'
				lincom $BEFORE + median_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': `reg'_m_`num'
				estadd local p_value `p_test': `reg'_m_`num'
							
			eststo `reg'_m_`num'_c:  quietly areg camp_exp_pc $BEFORE above_median_corrupt median_before $muni_controls $challenger_controls i.term if $var_diff == `num', absorb(codigo_uf) robust  cluster(id_city)
				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": `reg'_m_`num'_c
				estadd local mayor_municipal "\checkmark": `reg'_m_`num'_c
				lincom $BEFORE + median_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': `reg'_m_`num'_c
				estadd local p_value `p_test': `reg'_m_`num'_c
	 
			eststo `reg'_q_`num':  quietly areg camp_exp_pc $BEFORE quartile2 quartile3 quartile4 quart2_before quart3_before quart4_before i.term if $var_diff == `num', absorb(codigo_uf) robust cluster(id_city)
 				sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": `reg'_q_`num'
				lincom $BEFORE + quart4_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': `reg'_q_`num'
				estadd local p_value `p_test': `reg'_q_`num'

			eststo `reg'_q_`num'_c: quietly areg camp_exp_pc $BEFORE quartile2 quartile3 quartile4 quart2_before quart3_before quart4_before $muni_controls $challenger_controls i.term if $var_diff == `num', absorb(codigo_uf) robust  cluster(id_city)
			 	sum camp_exp_pc if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": `reg'_q_`num'_c
				estadd local mayor_municipal "\checkmark": `reg'_q_`num'_c
				lincom $BEFORE + quart4_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': `reg'_q_`num'_c
				estadd local p_value `p_test': `reg'_q_`num'_c

}	


capture label var before_broad "Before $\times$ corruption"
capture label var broad "Corruption"

capture label var corrupt_before "Before $\times$ share corruption"
capture label var fraction_broad "Share corruption"


label var before "Before"
local reg above_margin 
    		#delimit;
			esttab `reg'_m_0 `reg'_m_0_c
				   `reg'_q_0 `reg'_q_0_c
				   `reg'_m_1 `reg'_m_1_c
				   `reg'_q_1 `reg'_q_1_c
			using "tables and figures/table_7.tex", b(3) se compress label star(* 0.10 ** 0.05 *** 0.01) replace
			mtitles( "Median" "Median" "Quartile"  "Quartile"  "Median"  "Median"  "Quartile"  "Quartile") 
			keep($BEFORE above_median_corrupt median_before quart2_before quart3_before quart4_before quartile2 quartile3 quartile4)
			mgroups( "\shortstack{Against weak incumbent\\(head start $\approx$ 0)}"  "\shortstack{Against strong incumbent \\  (head start $>>$ 0)}", pattern(1 0 0 0 1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
 			stats(N mD mayor_municipal state_fe r2_a b0_b2 p_value, fmt(%9.0fc %9.3fc ) labels("Observations" "Dep. Var. Mean" "Mayor and municipal controls" "State and term FE" "Adjusted R$^2$" "$\beta_0+\beta_2 \, c_{ist}$" "p-value"))
			/*nonum*/ ar2 booktabs nonotes /*nomtitles*/ title(deletethis) 
			substitute(\begin{table}[htbp]\centering \centering \caption{deletethis} /*\footnotesize*/{ \end{tabular} \end{tabular}} \end{table} %);	
			#delimit cr

restore
		
		
* Table 8.
	global BEFORE before
	global corrupt broad

* Bolsa Familia
	local Other_reg "bolsa_pc valor"
		
	foreach dep_var of varlist `Other_reg' {
	    
			eststo m_c_`dep_var': quietly areg `dep_var' $BEFORE above_median_corrupt median_before $muni_controls $mayor_controls i.term, absorb(codigo_uf) robust  cluster(id_city)
				sum `dep_var' if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": m_c_`dep_var'
				estadd local mayor_municipal "\checkmark": m_c_`dep_var'
				lincom $BEFORE + median_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': m_c_`dep_var'
				estadd local p_value `p_test': m_c_`dep_var'
	
			eststo q_c_`dep_var': quietly areg `dep_var' $BEFORE quartile2 quartile3 quartile4 quart2_before quart3_before quart4_before $muni_controls $mayor_controls i.term , absorb(codigo_uf) robust  cluster(id_city)
				sum `dep_var' if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": q_c_`dep_var'
				estadd local mayor_municipal "\checkmark": q_c_`dep_var'
				lincom $BEFORE + quart4_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': q_c_`dep_var'
				estadd local p_value `p_test': q_c_`dep_var'								
	}
	
*  Public Employment
preserve
capture gen Before_1 = before_1
	replace Before_1 = 1 if Before_1 == 0 &  lottery == 25 & term ==2005 /* lotteria con fecha 09oct2007. 1 year antes*/
	label var Before_1 "Lottery occurred before the year previous of the election."

	replace before = Before_1
	
	global BEFORE before
	global corrupt broad
	
	local Other_reg "empl_perm_pc empl_trans_pc"
		
	foreach dep_var of varlist `Other_reg' {
	    	    
			eststo m_c_`dep_var': quietly areg `dep_var' $BEFORE above_median_corrupt median_before $muni_controls $mayor_controls i.term, absorb(codigo_uf) robust  cluster(id_city)
				sum `dep_var' if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": m_c_`dep_var'
				estadd local mayor_municipal "\checkmark": m_c_`dep_var'
				lincom $BEFORE + median_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': m_c_`dep_var'
				estadd local p_value `p_test': m_c_`dep_var'
	
			eststo q_c_`dep_var': quietly areg `dep_var' $BEFORE quartile2 quartile3 quartile4 quart2_before quart3_before quart4_before $muni_controls $mayor_controls i.term , absorb(codigo_uf) robust  cluster(id_city)
				sum `dep_var' if e(sample), mean 
				loc mymean: display %9.3f r(mean) 	
				estadd local mD `mymean', replace	
				estadd local state_fe "\checkmark": q_c_`dep_var'
				estadd local mayor_municipal "\checkmark": q_c_`dep_var'
				lincom $BEFORE + quart4_before
				local estimate: display %9.3f r(estimate)
				local p_test: display %9.3f r(p)
				estadd local b0_b2 `estimate': q_c_`dep_var'
				estadd local p_value `p_test': q_c_`dep_var'								
	}
	
restore
	
  eststo dir
	  
		#delimit;
		esttab 
			m_c_bolsa_pc q_c_bolsa_pc m_c_valor q_c_valor 
			m_c_empl_perm_pc q_c_empl_perm_pc m_c_empl_trans_pc q_c_empl_trans_pc
			using "tables and figures\table_8.tex", b(3) se compress label star(* 0.10 ** 0.05 *** 0.01) replace
			mtitles( "Median" "Quartile"  "Median" "Quartile" "Median" "Quartile" "Median" "Quartile") 
			mgroups( "\shortstack{Beneficiary families \\(\% population)}"   "Value of benefits" "Permanent employees" "Transitory employees"  , pattern(1 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
			keep($BEFORE above_median_corrupt median_before 
				 quart2_before quart3_before quart4_before quartile2 quartile3 quartile4)
			stats(N mD r2_a b0_b2 p_value, fmt(%9.0fc %9.3fc ) labels("Observations" "Dep. Var. Mean" "Adjusted R$^2$" "$\beta_0+\beta_2 \, c_{ist}$" "p-value"))
			/*nonum*/ ar2 booktabs nonotes /*nomtitles*/ title(deletethis) 
			substitute(\begin{table}[htbp]\centering \centering \caption{deletethis} /*\footnotesize*/{ \end{tabular} \end{tabular}} \end{table} %);
			#delimit cr

eststo clear	